﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Lib.Utility;
using Module.BusinessLogic;
using Module.Core.LoginSystem.Commons.Framwork;

namespace Web.Cms.Partners
{
    public partial class PartnersManager : AdminUserControl
    {
        #region Khai báo biến
        private int cPage = 1;
        private DateTime dateTo;
        private DateTime dateFrom;
        #endregion

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            GetRequest();
        }

        #region lấy giá trị khi request
        void GetRequest()
        {
            if (Request.QueryString["keyword"] != null || Request.QueryString["keyword"] != "")
                txtKeysearch.Text = Request.QueryString["keyword"];

            if (Request.QueryString["dateFrom"] != null && Request.QueryString["dateFrom"] != "")
            {
                dateFrom = DateTime.Parse(Request.QueryString["dateFrom"], CultureInfo.CreateSpecificCulture("fr-FR"));
                txtDatefrom.Text = dateFrom.ToString("dd/MM/yyyy");
            }
            else
            {
                dateFrom = DateTime.Now.AddMonths(-2);
                txtDatefrom.Text = dateFrom.ToString("dd/MM/yyyy");
            }

            if (Request.QueryString["dateTo"] != null && Request.QueryString["dateTo"] != "")
            {
                dateTo = DateTime.Parse(Request.QueryString["dateTo"], CultureInfo.CreateSpecificCulture("fr-FR"));
                txtDateTo.Text = dateTo.ToString("dd/MM/yyyy");
            }
            else
            {
                dateTo = DateTime.Now;
                txtDateTo.Text = dateTo.ToString("dd/MM/yyyy");
            }
        }
        #endregion

        #region OnPreRender
        protected override void OnPreRender(EventArgs e)
        {
            try
            {
                base.OnPreRender(e);
                LoadData();
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }

        }
        #endregion

        #region LoadData
        private void LoadData()
        {
            DataTable dt;
            dt = PartnerBL.Search(txtKeysearch.Text, dateFrom, dateTo.AddDays(+1));
            if (dt != null && dt.Rows.Count > 0)
            {
                grdData.DataSource = dt;
                grdData.RowDataBound += new GridViewRowEventHandler(grdData_RowDataBound);
                grdData.DataBind();
            }
            else
            {
                grdData.Visible = false;
                if (Request.QueryString["keyword"] != null && Request.QueryString["keyword"] != "")
                    lblMessage.Text = "Không tìm thấy dữ liệu";
                else
                    lblMessage.Text = "Dữ liệu đang được cập nhật";
            }

        }

        protected void grdData_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                switch (e.CommandName)
                {
                    case "Commanddelete":
                        int id = ConvertUtility.ToInt32(e.CommandArgument);
                        PartnerBL.Delete(id);
                        LoadData();
                        break;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        void grdData_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (e.Row.DataItemIndex + 1).ToString();
            }
        }


        #endregion

        #region tìm kiếm
        protected void btnTimKiem_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Cms/Default.aspx?ctrlkey=PartnersManager&keyword=" + txtKeysearch.Text.Trim() + "&dateFrom=" + txtDatefrom.Text.Trim() + "&dateTo=" + txtDateTo.Text.Trim());

        }
        #endregion
    }
}